package com.example.jucdemo.synchdemo;

/**
 * @Author: Dijah
 * @Date: 2023/3/17 15:05
 * @Desc:
 * 场景：
 *  两个线程同时访问两个对象的锁
 *
 * 这个是不安全的
 *
 */
public class demo2 implements Runnable{

    static demo2 instance1 = new demo2();
    static demo2 instance2 = new demo2();

    @Override
    public void run() {
        method();
    }

    private synchronized void method(){
        System.out.println("thread name : " + Thread.currentThread().getName() + ",start");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("thread name : " + Thread.currentThread().getName() + ",end");
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(instance1);
        Thread thread2 = new Thread(instance2);

        thread1.start();
        thread2.start();

        while (thread1.isAlive() || thread2.isAlive()){

        }
        System.out.println("test over");
    }
}
